/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @Original Author: jin Chen
 * @date: Mar 23, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.visualclassifier.classify.v2.agent;

import edu.psu.geovista.classification.ClassificationResult;
import edu.psu.geovista.classification.setting.ClassifySetting;
import edu.psu.geovista.classification.setting.ClassifySetting1D;
import edu.psu.geovista.classification.setting.ClassifySettingSwitch;
import edu.psu.geovista.data.geog.DataSetForApps;
import edu.psu.geovista.ui.event.ClassificationResultEvent;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class MapClassifyAgent extends ClassifyAgent {
    public static final String  ITEM_NONE="none";
    public static final String ITEM_NONE_DESC="Uni-var map. Select any other itmes to switch to Bi-var map";

    private String lastyvar;//last selection of y variable
    public MapClassifyAgent(ClassificationInterestor host) {
        super(host);
    }

    public void setDataSet(DataSetForApps dataSet) {
        super.setDataSet(dataSet);
        setActonComboBox(false);   // 
        ycb.insertItemAt(ITEM_NONE,0);
        setActonComboBox(true);
     }
     public void setDataModel(edu.psu.geovista.data.model.AppDataModel datamodel){
         super.setDataModel(datamodel);
         setActonComboBox(false);   // 
        ycb.insertItemAt(ITEM_NONE,0);
        ycb.addItemDesc(ITEM_NONE,ITEM_NONE_DESC);
        this.setLastyvar((String) ycb.getSelectedItem() );
        setActonComboBox(true);
     }



    public void classificationResultChanged(ClassificationResultEvent e) {
            ClassificationResult cr = e.getClassificationResult();
            if(cr==null)return;
            super.classificationResultChanged(e);

            int numOfDimension = cr.getNumberOfAxis(); //  
            if(numOfDimension==1){
                //this.setYEnable(false);
                setActonComboBox(false);
                ycb.setSelectedItem(ITEM_NONE);
                setActonComboBox(true);

            }
            else{
               // this.setYEnable(true);
            }
        }
       public void actionPerformed(ActionEvent e) {
           super.actionPerformed(e);
           this.setLastyvar((String) ycb.getSelectedItem());
       }



    protected ClassifySetting[] createSetting(Object source){

            //ClassifySetting stg=null;
            ClassifySetting[] stgs=null;
            //String var=null;
            if(source==getXcb()){//select a  x variable,
                ClassifySetting1D setting=new ClassifySetting1D();
                setting.setDimensionName(ClassifySetting1D.DIMENSION_NAME_X);
                String var = (String) getXcb().getSelectedItem();
                setting.setVariableName(var);
                stgs=new ClassifySetting[]{setting};
            }
            else if (source==getYcb()){//select a y variable
                JComboBox ycb=getYcb();
                String  var = (String) ycb.getSelectedItem();
                if(var.equals(ITEM_NONE) ){
                     ClassifySettingSwitch setting=new ClassifySettingSwitch(ClassifySettingSwitch.UNI_VAR );//uni-var
                     stgs=new ClassifySetting[]{setting};
                }
                else{ //selected a item other than ITEM_NONE
                    String lastyvar = this.getLastyvar();
                    if(lastyvar!=null&&lastyvar.equals(ITEM_NONE) ) {
                        //just switch from uni-var to bi-var
                        ClassifySetting stg1=new ClassifySettingSwitch(ClassifySettingSwitch.BI_VAR );
                        ClassifySetting1D stg2=new ClassifySetting1D();// 
                        stg2.setDimensionName(ClassifySetting1D.DIMENSION_NAME_Y);
                        stg2.setVariableName(var);
                        stgs=new ClassifySetting[]{stg1,stg2};

                    }
                    else{//simply select diff variable for bi-var classification
                        ClassifySetting1D setting=new ClassifySetting1D();
                        setting.setDimensionName(ClassifySetting1D.DIMENSION_NAME_Y);
                        setting.setVariableName(var);
                        stgs=new ClassifySetting[]{setting};
                    }
                }


            }

            return stgs;
       }

    public String getLastyvar() {
        return lastyvar;
    }

    public void setLastyvar(String lastyvar) {
        this.lastyvar = lastyvar;
    }
}
